package com.atstudy.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2022/12/30 10:43
 * @copyright: 博为峰
 */
public class SeverDemo {
    public static void main(String[] args) throws IOException {
        //端口 65535
        //创建服务器端对象
        ServerSocket serverSocket = new ServerSocket(8000);
        //获取客户端连接
        Socket socket = serverSocket.accept();//阻塞

        //客户端IP
        String ip = socket.getInetAddress().getHostAddress();
        //通过Socket获取客户端发来的数据（输入流-读）
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len = inputStream.read(bytes);
        String str = new String(bytes, 0, len);
        System.out.println("欢迎[" + ip + "]：" + str);

        //写数据到客户端
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("welcome".getBytes());

        socket.close();
        serverSocket.close();
    }

}
